package com.home.biz;

import com.github.pagehelper.PageHelper;
import com.home.base.biz.BaseBiz;
import com.home.base.exceptions.ServiceException;
import com.home.config.InitDataConfig;
import com.home.entity.ActivityPhone;
import com.home.entity.FetchPhone;
import com.home.entity.ProvInfo;
import com.home.mapper.ActivityPhoneMapper;
import com.home.mapper.FetchPhoneMapper;
import com.home.task.FetchTask;
import com.home.task.FetchTask2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/19 15:58
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class ActivityBiz extends BaseBiz<ActivityPhoneMapper, ActivityPhone> {

    @Autowired
    FetchTask ftch;
    @Autowired
    FetchTask2 fetchTask22;

    /**

     * @param limit  条数
     * @param offset  页码
     * @return
     */
    public List<ActivityPhone> list(int limit, int offset, String phoneNum, String cityName){
        PageHelper.startPage(offset, limit);
        Example example = new Example(ActivityPhone.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(phoneNum)){
            criteria.andLike("phoneNum","%"+phoneNum+"%");
        }

        example.setOrderByClause(" update_time desc");
        return selectByExample(example);

    }

    public void fetchOne(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"一生一号","2003130012433867",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchEight(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"4G飞享18元档","1905170011452295",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchEightFen(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"4G飞享(分销)18元档","1910230012354413",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchJia(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"5G超级家合约版","2001190012394435",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchJiaSix(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"超级家合约版68元档","2002210012410791",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchJiaEight(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"超级家68元档","1905170011452298",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchJiaOne(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"5G超级家169元档","2001150012392387",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchXin(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"万能随心卡","1811060009745646",sNum,eNum,tNum);
        }

        //结束
    }

    public void fetchYu(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            fetchTask22.activityTask(this,provName,cityName,"渝乐星卡19元档","2001200012395003", Arrays.asList("135","178"),tNum);
        }

        //结束
    }


    public void fetchZs(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();

        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            ftch.activityTask(this,provName,cityName,"专属幸运号","1910170012348890",sNum,eNum,tNum);
        }

        //结束
    }



    public void deletePhone(){
        mapper.deletePhone();
    }

    public Integer counts(){
        return mapper.phoneCounts();
    }

    public void saveList(String city,String province,String method,List<String> phone) {
        if(phone==null){
            return;
        }
        for(String p:phone){
            save(ActivityPhone.builder().cityName(city).provName(province).fetchMethod(method).phoneNum(p).build());
        }

    }

    public void save(ActivityPhone fetchPhone) {

        try {
            insertSelective(fetchPhone);
        } catch (Exception e) {
            //e.printStackTrace();
        }

    }
}
